<?php
/**
 * 公共函数
 *
 * User: long
 * Date: 15/6/12
 * Time: 15:48
 */

/**
 *
 * 字符截取
 * @param string $string
 * @param int $start
 * @param int $length
 * @param string $charset
 * @param string $dot
 *
 * @return string
 */
function str_cut(&$string, $start, $length, $charset = "utf-8", $dot = '...')
{
  if (function_exists('mb_substr')) {
	if (mb_strlen($string, $charset) > $length) {
	  return mb_substr($string, $start, $length, $charset) . $dot;
	}
	return mb_substr($string, $start, $length, $charset);

  } else if (function_exists('iconv_substr')) {
	if (iconv_strlen($string, $charset) > $length) {
	  return iconv_substr($string, $start, $length, $charset) . $dot;
	}
	return iconv_substr($string, $start, $length, $charset);
  }

  $charset = strtolower($charset);
  switch ($charset) {
	case "utf-8" :
	  preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $ar);
	  if (func_num_args() >= 3) {
		if (count($ar[0]) > $length) {
		  return join("", array_slice($ar[0], $start, $length)) . $dot;
		}
		return join("", array_slice($ar[0], $start, $length));
	  } else {
		return join("", array_slice($ar[0], $start));
	  }
	  break;
	default:
	  $start = $start * 2;
	  $length = $length * 2;
	  $strlen = strlen($string);
	  for ($i = 0; $i < $strlen; $i++) {
		if ($i >= $start && $i < ($start + $length)) {
		  if (ord(substr($string, $i, 1)) > 129) $tmpstr .= substr($string, $i, 2);
		  else $tmpstr .= substr($string, $i, 1);
		}
		if (ord(substr($string, $i, 1)) > 129) $i++;
	  }
	  if (strlen($tmpstr) < $strlen) $tmpstr .= $dot;

	  return $tmpstr;
  }
}

/**
 * 生成分页链接
 *
 * @param int $page_count
 * @param int $curr_page
 * @param string $url
 *
 * @return string
 */
function page_links($page_count, $curr_page, $url = "")
{

  if ($url != "") {
	$url .= "&page=";
  } else {
	$url = "?page=";
  }

  $curr_class = "current";
  $output = '<div class="pages">';
  if ($curr_page != 1) {
	$output .= '<a class="first" href="' . $url . '1">首页</a>';
	$output .= '<a class="prev" href="' . $url . ($curr_page - 1) . '">上一页</a>';
  }

  if ($curr_page < 6 || $page_count < 10) {
	for ($i = 1; $i < 10; $i++) {
	  if ($i != $curr_page) {
		$output .= "<a href=\"" . $url . $i . "\" class=\"";
		$output .= $curr_page == $i ? $curr_class : '';
		$output .= "\">{$i}</a>";
	  } else {
		$output .= '<a class="' . $curr_class . '">' . $curr_page . '</a>';
	  }
	  if ($i >= $page_count)
		break;
	}
	if ($page_count > 10)
	  $output .= "<a href='" . $url . "8'>...</a>";

  } elseif ($curr_page > 5 && $curr_page < $page_count - 4) {
	$output .= "<a href='" . $url . ($curr_page - 5) . "'>...</a>";
	for ($i = $curr_page - 4; $i < $curr_page + 5; $i++) {
	  $output .= "<a href=\"" . $url . $i . "\" class=\"";
	  $output .= $curr_page == $i ? $curr_class : '';
	  $output .= "\">{$i}</a>";
	  if ($i >= $page_count)
		break;
	}
	$output .= "<a href=\"" . $url . ($curr_page + 6) . "\">...</a>";

  } elseif ($curr_page + 5 > $page_count) {
	$output .= "<a href=\"" . $url . ($curr_page - 9) . "\">...</a>";
	for ($i = $page_count - 8; $i < $page_count + 1; $i++) {
	  if ($i > 1) {
		$output .= "<a href=\"" . $url . $i . "\" class=\"";
		$output .= $curr_page == $i ? $curr_class : '';
		$output .= "\">{$i}</a>";
	  }
	}
	if ($curr_page < $page_count - 6) {
	  $output .= "<a href='" . $url . ($curr_page + 5) . "'>...</a>";
	}
  }

  if ($curr_page < $page_count) {
	$output .= '<a class="next" href="' . $url . ($curr_page + 1) . '">下一页</a>';
	$output .= '<a class="first" href="' . $url . $page_count . '">末页</a>';
  }

  $output .= '</div>';

  return $output;
}


/**
 * 后台页面分页链接
 *
 * @param int $page_count
 * @param int $curr_page
 * @param string $url
 *
 * @return string
 */
function cms_page_links($page_count, $curr_page, $url = "")
{

  if ($url != "") {
	$url .= "&page=";
  } else {
	$url = "?page=";
  }

  $curr_class = "active";
  $output = '<ul class="pagination">';
  if ($curr_page != 1) {
	$output .= '<li><a class="first" href="' . $url . '1">首页</a></li>';
	$output .= '<li><a class="prev" href="' . $url . ($curr_page - 1) . '">上一页</a></li>';
  }

  if ($curr_page < 6 || $page_count < 10) {
	for ($i = 1; $i < 10; $i++) {
	  if ($i != $curr_page) {
		$output .= "<li><a href=\"" . $url . $i . "\" class=\"";
		$output .= $curr_page == $i ? $curr_class : '';
		$output .= "\">{$i}</a></li>";
	  } else {
		$output .= '<li><a class="' . $curr_class . '">' . $curr_page . '</a></li>';
	  }
	  if ($i >= $page_count)
		break;
	}
	if ($page_count > 10)
	  $output .= "<li><a href='" . $url . "8'>...</a></li>";

  } elseif ($curr_page > 5 && $curr_page < $page_count - 4) {
	$output .= "<li><a href='" . $url . ($curr_page - 5) . "'>...</a></li>";
	for ($i = $curr_page - 4; $i < $curr_page + 5; $i++) {
	  $output .= "<li><a href=\"" . $url . $i . "\" class=\"";
	  $output .= $curr_page == $i ? $curr_class : '';
	  $output .= "\">{$i}</a></li>";
	  if ($i >= $page_count)
		break;
	}
	$output .= "<li><a href=\"" . $url . ($curr_page + 6) . "\">...</a></li>";

  } elseif ($curr_page + 5 > $page_count) {
	$output .= "<li><a href=\"" . $url . ($curr_page - 9) . "\">...</a></li>";
	for ($i = $page_count - 8; $i < $page_count + 1; $i++) {
	  if ($i > 1) {
		$output .= "<li><a href=\"" . $url . $i . "\" class=\"";
		$output .= $curr_page == $i ? $curr_class : '';
		$output .= "\">{$i}</a></li>";
	  }
	}
	if ($curr_page < $page_count - 6) {
	  $output .= "<li><a href='" . $url . ($curr_page + 5) . "'>...</a></li>";
	}
  }

  if ($curr_page < $page_count) {
	$output .= '<li><a class="next" href="' . $url . ($curr_page + 1) . '">下一页</a></li>';
	$output .= '<li><a class="first" href="' . $url . $page_count . '">末页</a></li>';
  }

  $output .= '</ul>';

  return $output;
}

/**
 * 显示提示消息
 *
 * @param string $msg
 */

function alert($msg = "", $url = "")
{
  echo '<script type="text/javascript">';
  echo 'alert("' . $msg . '");';

  if ($url == "") {
	echo "history.go(-1)";
  } else {
	echo 'location.href="' . $url . '";';
  }
  echo '</script>';
  exit;
}

/**
 * function 分页配置函数
 * param    string        $total            总数据量
 * param    string        $current          当前页码
 * param    string        $limit            单页数据量
 * param    string        $size             显示数字页码
 * param    array        $conf              分页配置(功能)
 * 默认全部开启 '首页start','尾页end','上页prev','下页next','下拉框jump2';
 * param    string        $style            分页样式
 * return     string      $pageStyle        返回分页
 */


function pagination($total, $current, $limit, $size = null, $conf = null, $style = 'zidingyi')
{

  //总页码
  $sum = ceil($total / $limit);

  //判断当前页的范围
  if ($current <= 0) {

	$current = 1;

  } else if ($current >= $sum) {

	$current = $sum;

  } else {

	$current += 0;

  }


  //地址栏上页码的变量
  $var = 'page';

  //获取地址
  $url = pagingUrl($var);

  //判断是否有定制功能，没有就默认全部功能显示
  if (!$conf) {

	$conf = array('start', 'end', 'prev', 'next', 'jump2');

  }

  $pageStyle = "<div id='page' class='" . $style . "'>";

  //判断是否是第一页，是则上一页或首页不能点
  if ($current != 1) {

	//判断是否定制首页功能
	if (in_array('start', $conf)) {

	  $pageStyle .= "<a href='" . $url . $var . "=1'>首页</a>";

	}

	//判断是否定制上一页功能
	if (in_array('prev', $conf)) {

	  $pageStyle .= "<a href='" . $url . $var . "=" . ($current - 1) . "'>上一页</a>";

	}

  } else {

	//判断是否定制首页功能
	if (in_array('start', $conf)) {

	  $pageStyle .= "<span class='disabled'>首页</span>";

	}

	//判断是否定制上一页功能
	if (in_array('prev', $conf)) {

	  $pageStyle .= "<span class='disabled'>上一页</span>";

	}

  }

  //判断是否定制了数字页码
  if ($size) {
	//左边最小页码
	$begin = $current - floor($size / 2);
	//左边最大页码
	$finish = $current + floor($size / 2);


	//防止出现负数页码
	if ($begin < 1) {

	  $begin = 1;

	  $finish = $size;

	  //防止页码超过总页数
	} else if ($finish > $sum) {

	  $begin = $sum - $size;

	  $finish = $sum;

	}

	#当总页码小于每页显示的页码时
	if ($sum < $size) {
	  $begin = 1;
	  $finish = $sum;

	}

	//循环输出页码，$begin是页码的左边开始，$finish页码的右边结束
	for ($i = $begin; $i <= $finish; $i++) {

	  //判断是否是当前页码，是则不能点
	  if ($i != $current) {

		$pageStyle .= "<a href='" . $url . $var . "=" . $i . "'>" . $i . "</a>";

	  } else {

		$pageStyle .= "<span class='current'>" . $i . "</span>";

	  }
	}

  }

  //判断是否是末页，是则下一页或者末页不能点
  if ($current != $sum) {

	//判断是否定制下一页功能
	if (in_array('next', $conf)) {

	  $pageStyle .= "<a href='" . $url . $var . "=" . ($current + 1) . "'>下一页</a>";

	}

	//判断是否定制末页功能
	if (in_array('end', $conf)) {

	  $pageStyle .= "<a href='" . $url . $var . "=" . $sum . "'>末页</a>";

	}

  } else {

	//判断是否定制下一页功能
	if (in_array('next', $conf)) {

	  $pageStyle .= "<span class='disabled'>下一页</span>";

	}

	//判断是否定制末页功能
	if (in_array('end', $conf)) {

	  $pageStyle .= "<span class='disabled'>末页</span>";

	}

  }
  //判断是否定制页面任意跳转功能，分两种跳转方式
  if (in_array('jump', $conf)) {

	$pageStyle .= "<select id='jump'>";

	for ($i = 1; $i <= $sum; $i++) {

	  if ($i != $current) {

		$pageStyle .= "<option  value=" . $url . $var . "=" . ($i) . ">$i</option>";

	  } else {

		$pageStyle .= "<option selected='selected' value=" . $url . $var . "=" . ($i) . ">$i</option>";

	  }
	}

	$pageStyle .= "</select>";

	$pageStyle .= "<script>\n";
	$pageStyle .= "var sel = document.getElementById('jump');";
	$pageStyle .= "sel.onchange=function(){";
	$pageStyle .= "window.location.href=this.value";
	$pageStyle .= "}";
	$pageStyle .= "</script>";

  } else if (in_array('jump2', $conf)) {

	$pageStyle .= "<input class='text' type='text' id='jump2Page' onkeyup='this.value=this.value.replace(/[^\d]*/g,\"\")' name='" . $var . "'>";
	$pageStyle .= "<a href='' class='button' id='jump2'>go</a>";
	$pageStyle .= "<script>\n";
	$pageStyle .= "var sel = document.getElementById('jump2');";
	$pageStyle .= "sel.onclick=function(){";
	$pageStyle .= "var p = document.getElementById('jump2Page').value;";
	$pageStyle .= "this.href=('" . $url . $var . "=" . "'+(p?p:1))";
	$pageStyle .= "}";
	$pageStyle .= "</script>";

  }
  $pageStyle .= "<span class='desc'>共 " . $sum . "页" . $total . "条记录</span></div>";

  return $pageStyle;

}

/**
 * 获得文件扩展名
 *
 * @param $filename
 * @return string
 */
function fextname($filename)
{
  return substr($filename, strrpos($filename, "."));
}

/**
 * 是否是Email
 */
function isemail()
{

}

